﻿using System.Windows.Forms;
using DevExpress.Utils.Controls;
using DevExpress.Utils;
using System.Drawing;
using MessageBox = MicropleSoft.Controls.DialogBox.MessageBox;

namespace MicropleSoft.Controls.DialogBox
{
    /// <summary>
    /// Custom MessageBox Wrapper
    /// </summary>
    public class CustomMessageBox
    {
        public static Image GetImage(MsgBoxIcons msgBoxIcon)
        {
            ImageCollection ic = ImageHelper.CreateImageCollectionFromResources(
                "MicropleSoft.Controls.DialogBox.Images." + msgBoxIcon.ToString() + ".png", typeof(CustomMessageBox).Assembly, new Size(128, 128));
            if (ic.Images.Count == 0)
                return null;
            return ic.Images[0];
        }
        private CustomMessageBox()
        { }
        /// <summary>
        /// 指定消息框上出现哪些按钮的组合
        /// </summary>
        public enum MsgBoxButtons
        {
            /// <summary>
            /// OK按钮
            /// </summary>
            Ok = 0,
            /// <summary>
            /// OK按钮以及Cancel按钮
            /// </summary>
            OkCancel,
            /// <summary>
            /// OK按钮, No按钮, 以及Cancel按钮
            /// </summary>
            OkNoCancel,
        }
        /// <summary>
        /// 指定消息框上出现的图标
        /// </summary>
        public enum MsgBoxIcons
        {
            /// <summary>
            /// 成功标志
            /// </summary>
            Good = 0,
            /// <summary>
            /// 错误标志
            /// </summary>
            Error,
            /// <summary>
            /// 提示性标志
            /// </summary>
            Info,
            /// <summary>
            /// 询问标志
            /// </summary>
            Question,
            /// <summary>
            /// 不要出现任何标志
            /// </summary>
            Warn,
            /// <summary>
            /// 警告标志
            /// </summary>
            None,
        }

        // 跨线程使用之必须
        private delegate DialogResult ShowItDelegate(Form owner);
        private static DialogResult ShowIt(Form owner, MessageBox mbx)
        {
            if (owner != null
                && owner.InvokeRequired)
            {
                var d = new ShowItDelegate(mbx.ShowDialog);
                return (DialogResult)owner.Invoke(d, owner);
            }

            return mbx.ShowDialog(owner);
        }

        #region "overloads of Show ..."

        /// <summary>
        /// 重载0.0: 显示自定义的消息框, 指定owner
        /// </summary>
        /// <param name="owner">宿主窗体</param>
        /// <param name="msg">消息文本</param>
        /// <param name="caption">消息标题</param>
        /// <param name="btns">要在界面上显示的按钮组合</param>
        /// <param name="icon">要在界面上显示的图标</param>
        /// <returns></returns>
        public static DialogResult Show(
                Form owner,
                string msg, string caption,
                MsgBoxButtons btns, MsgBoxIcons icon)
        {

            using (var mbx = new MessageBox())
            {
                mbx.SetIcon(icon);
                mbx.SetCaption(caption);
                mbx.SetMsg(msg);
                mbx.LayoutBtns(btns);
                MessageBox.DoBeep(icon);

                return ShowIt(owner, mbx);
            }
        }

        /// <summary>
        /// 重载0.1: 显示自定义的消息框, 不指定owner
        /// </summary>
        /// <param name="msg">消息文本</param>
        /// <param name="caption">消息标题</param>
        /// <param name="btns">要在界面上显示的按钮组合</param>
        /// <param name="icon">要在界面上显示的图标</param>
        /// <returns></returns>
        public static DialogResult Show(
                string msg, string caption,
                MsgBoxButtons btns, MsgBoxIcons icon)
        {
            return Show(null, msg, caption, btns, icon);
        }

        /// <summary>
        /// 重载1.1: 使用默认的OK按钮, 指定owner
        /// </summary>
        /// <param name="owner"></param>
        /// <param name="msg">消息文本</param>
        /// <param name="caption">消息标题</param>
        /// <param name="icon">要在界面上显示的图标</param>
        /// <returns></returns>
        public static DialogResult Show(Form owner,
            string msg, string caption, MsgBoxIcons icon)
        {
            return Show(owner, msg, caption, MsgBoxButtons.Ok, icon);
        }
        /// <summary>
        /// 重载1.2: 使用默认的OK按钮
        /// </summary>
        /// <param name="msg">消息文本</param>
        /// <param name="caption">消息标题</param>
        /// <param name="icon">要在界面上显示的图标</param>
        /// <returns></returns>
        public static DialogResult Show(
            string msg, string caption, MsgBoxIcons icon)
        {
            return Show(null, msg, caption, MsgBoxButtons.Ok, icon);
        }

        /// <summary>
        /// 重载2.1: 使用默认的消息标题, 指定owner
        /// </summary>
        /// <param name="owner"></param>
        /// <param name="msg">消息文本</param>
        /// <param name="btns">要在界面上显示的按钮组合</param>
        /// <param name="icon">要在界面上显示的图标</param>
        /// <returns></returns>
        public static DialogResult Show(Form owner,
            string msg, MsgBoxButtons btns, MsgBoxIcons icon)
        {
            return Show(owner, msg, "Message", btns, icon);
        }
        /// <summary>
        /// 重载2.2: 使用默认的消息标题
        /// </summary>
        /// <param name="msg">消息文本</param>
        /// <param name="btns">要在界面上显示的按钮组合</param>
        /// <param name="icon">要在界面上显示的图标</param>
        /// <returns></returns>
        public static DialogResult Show(
            string msg, MsgBoxButtons btns, MsgBoxIcons icon)
        {
            return Show(null, msg, "Message", btns, icon);
        }

        /// <summary>
        /// 重载3.1: 使用默认的OK按钮, 默认的图标, 指定owner
        /// </summary>
        /// <param name="owner"></param>
        /// <param name="msg">消息文本</param>
        /// <param name="caption">消息标题</param>
        /// <returns></returns>
        public static DialogResult Show(Form owner,
            string msg, string caption)
        {
            return Show(owner, msg, caption, MsgBoxIcons.Info);
        }
        /// <summary>
        /// 重载3.2: 使用默认的OK按钮, 默认的图标
        /// </summary>
        /// <param name="msg">消息文本</param>
        /// <param name="caption">消息标题</param>
        /// <returns></returns>
        public static DialogResult Show(string msg, string caption)
        {
            return Show(null, msg, caption, MsgBoxIcons.Info);
        }

        /// <summary>
        /// 重载4.1: 使用默认的OK按钮, 默认的消息标题, 指定owner
        /// </summary>
        /// <param name="owner"></param>
        /// <param name="msg">消息文本</param>
        /// <param name="icon">要在界面上显示的图标</param>
        /// <returns></returns>
        public static DialogResult Show(Form owner,
            string msg, MsgBoxIcons icon)
        {
            return Show(owner, msg, "Message", icon);
        }
        /// <summary>
        /// 重载4.2: 使用默认的OK按钮, 默认的消息标题
        /// </summary>
        /// <param name="msg">消息文本</param>
        /// <param name="icon">要在界面上显示的图标</param>
        /// <returns></returns>
        public static DialogResult Show(string msg, MsgBoxIcons icon)
        {
            return Show(null, msg, "Message", icon);
        }

        /// <summary>
        /// 重载5.1: 使用默认的OK按钮, 默认的消息标题, 默认的图标, 指定owner
        /// </summary>
        /// <param name="owner"></param>
        /// <param name="fmt">格式串或字符串</param>
        /// <param name="args">参数</param>
        /// <returns></returns>
        public static DialogResult Show(Form owner,string fmt, params object[] args)
        {
            string msg = string.Format(fmt, args);
            return Show(owner, msg, MsgBoxIcons.Info);
        }
        /// <summary>
        /// 重载5.2: 使用默认的OK按钮, 默认的消息标题, 默认的图标
        /// </summary>
        /// <param name="fmt">格式串或字符串</param>
        /// <param name="args">参数</param>
        /// <returns></returns>
        public static DialogResult Show(string fmt, params object[] args)
        {
            string msg = string.Format(fmt, args);
            return Show((Form)null, msg, MsgBoxIcons.Info);
        }

        #endregion

        #region 再次封装
        public static void ShowInfo(string info)
        {
            Show(null, info, "提示", MsgBoxIcons.Info);
        }

        public static void ShowError(string errorInfo)
        {
            Show(null, errorInfo, "错误", MsgBoxIcons.Error);
        }

        public static bool ShowOkCancel(string msg)
        {
            return DialogResult.OK == Show(null, msg, "提问", MsgBoxButtons.OkCancel, MsgBoxIcons.Error);
        }

        public static DialogResult ShowOkNoCancel(string msg)
        {
            return Show(null, msg, "提问", MsgBoxButtons.OkNoCancel, MsgBoxIcons.Error);
        }
        #endregion

    }
}
